package com.yc.utilslib.reader.card;

import android.os.Parcel;
import android.os.Parcelable;

import com.yc.utilslib.utils.ByteUtils;
import com.yc.utilslib.utils.DateUtils;

import java.util.Date;

/**
 * 作者：TanTun
 * 时间：2017/2/26
 * 邮箱：32965926@qq.com
 * 描述：卡上用户信息块实体类
 */

public class UserInfo implements Parcelable {
    private byte[] mBlockData;

    private int mCardId;
    private int mCardType;
    private String mPassWord;
    private Date mValidDate;
    private int mRechargeCount;

    /**
     * 用户信息构造函数
     *
     * @param data 余额块的数据
     */
    private UserInfo(byte[] data) {
        this.mBlockData = data;

        mCardId = ((int) data[0] << 16) | ((int) data[2] << 8) | (int) data[1];
        mCardType = ByteUtils.byteArrayToInt(data, 3, 1);
        mPassWord = ByteUtils.byteArrayToBcdString(data, 4, 2);

        try {
            mValidDate = DateUtils.YYYYMMDD_SHORT.parse(ByteUtils
                    .byteArrayToBcdString(DateUtils.convertVailDateBytes(data, 6)));
        } catch (java.text.ParseException e) {
            e.printStackTrace();
            mValidDate = new Date(System.currentTimeMillis());
        }

        mRechargeCount = ByteUtils.byteArrayToInt(data, 8, 2, ByteUtils.Endian.Big);
    }

    protected UserInfo(Parcel in) {
        mBlockData = in.createByteArray();
        mCardId = in.readInt();
        mCardType = in.readInt();
        mPassWord = in.readString();
        mValidDate = (Date) in.readSerializable();
        mRechargeCount = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(mBlockData);
        dest.writeInt(mCardId);
        dest.writeInt(mCardType);
        dest.writeString(mPassWord);
        dest.writeSerializable(mValidDate);
        dest.writeInt(mRechargeCount);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<UserInfo> CREATOR = new Creator<UserInfo>() {
        @Override
        public UserInfo createFromParcel(Parcel in) {
            return new UserInfo(in);
        }

        @Override
        public UserInfo[] newArray(int size) {
            return new UserInfo[size];
        }
    };

    /**
     * 根据传入的数据构造一个新的实例
     *
     * @param data 余额块的数据
     * @return 用户信息
     */
    static UserInfo getInstance(byte[] data) {
        return new UserInfo(data);
    }

    /**
     * @return 块原始数据
     */
    public byte[] getBlockData() {
        return mBlockData;
    }

    /**
     * @return 卡流水号
     */
    public int getCardId() {
        return mCardId;
    }

    /**
     * @param cardId 卡流水号
     */
    public void setCardId(int cardId) {
        this.mCardId = cardId;
    }

    /**
     * @return 用户卡类
     */
    public int getCardType() {
        return mCardType;
    }

    /**
     * @param cardType 用户卡类
     */
    public void setCardType(int cardType) {
        this.mCardType = cardType;
    }

    /**
     * @return 超额密码
     */
    public String getPassWord() {
        return mPassWord;
    }

    /**
     * @param passWord 超额密码
     */
    public void setPassWord(String passWord) {
        this.mPassWord = passWord;
    }

    /**
     * @return 卡片有效期
     */
    public Date getVaildDate() {
        return mValidDate;
    }

    /**
     * @param validDate 超额密码
     */
    public void setValidDate(Date validDate) {
        this.mValidDate = validDate;
    }

    /**
     * @return 充值次数
     */
    public int getRechargeCount() {
        return mRechargeCount;
    }

    /**
     * @param rechargeCount 超额密码
     */
    public void setRechargeCount(int rechargeCount) {
        this.mRechargeCount = rechargeCount;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(50);
        sb.append("卡流水号：").append(mCardId).append(System.getProperty("line.separator"));
        sb.append("用户卡类：").append(mCardType).append(System.getProperty("line.separator"));
        sb.append("超额密码：").append(mPassWord).append(System.getProperty("line.separator"));
        sb.append("卡有效期：").append(DateUtils.YYYYMMDD_LONG.format(mValidDate))
                .append(System.getProperty("line.separator"));
        sb.append("充值次数：").append(mRechargeCount);
        return sb.toString();
    }
}
